package cn.com.wdjedu.androidapp.wjeduandroid;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.wdjedu.androidapp.wjeduandroid.mvp.MvpActivity;
import cn.com.wdjedu.androidapp.wjeduandroid.mvp.main.MainPresenter;
import cn.com.wdjedu.androidapp.wjeduandroid.ui.mainfragment.MainFragment;
import cn.com.wdjedu.androidapp.wjeduandroid.ui.societyfragment.SocietyFragment;
import cn.com.wdjedu.androidapp.wjeduandroid.ui.userfragment.UserFragment;

/**
 * Created by samson on 26/11/2016.
 */

public class MainActivity extends MvpActivity<MainPresenter> {

    private static final String FIRST_FRAGMENT = "FIRST_FRAGMENT";
    private static final String SECOND_FRAGMENT = "SECOND_FRAGMENT";
    private static final String THIRD_FRAGMENT = "THIRD_FRAGMENT";
    private static final String Four_FRAGMENT = "Four_FRAGMENT";
    public static final String TABS[] = {FIRST_FRAGMENT, SECOND_FRAGMENT, THIRD_FRAGMENT, Four_FRAGMENT};
    private boolean isExit = false;

    @BindView(R.id.tabhost)
    FragmentTabHost mTabHost;

    public static HashMap<String, Integer> mTabMap;
    LayoutInflater mLayoutInflater;

 //   private RefWatcher refWatcher;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        //检测内存溢出
//        refWatcher = LeakCanary.androidWatcher(getApplicationContext(),
//                new ServiceHeapDumpListener(getApplicationContext(), DisplayLeakService.class),
//                AndroidExcludedRefs.createAppDefaults().build());
//        refWatcher.watch(this);
        ButterKnife.bind(this);
        updateTitle("东方标准X");
//        if(! isNetWorkConnected(getApplication())){
//            Toast.makeext(getApplic、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、ation(),"没有网络！！！",Toast.LENGTH_LONG).show();
//        }
        initview();
    }

    @Override
    public void initview() {
        mTabMap = new HashMap<>();
        mTabMap.put(FIRST_FRAGMENT, 0);
        mTabMap.put(SECOND_FRAGMENT, 1);
        mTabMap.put(THIRD_FRAGMENT, 2);
        //  mTabMap.put(Four_FRAGMENT, 2);
        mLayoutInflater = LayoutInflater.from(getApplicationContext());

        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.getTabWidget().setMinimumHeight(100);// 设置tab的高度
        mTabHost.getTabWidget().setDividerDrawable(null);


        TabHost.TabSpec tabSpec = mTabHost.newTabSpec(TABS[0]);

        View tabView1 = mLayoutInflater.inflate(R.layout.activity_main_tab_item, null, false);
        final ImageView tabImage1 = (ImageView) tabView1.findViewById(R.id.tab_image);
        final TextView tabText1 = (TextView) tabView1.findViewById(R.id.tab_text);
        tabImage1.setImageResource(R.drawable.home_selected);
        tabText1.setText("东方标准X");
        tabText1.setTextColor(tabText1.getResources().getColor(R.color.txtselect));
        tabSpec.setIndicator(tabView1);
        mTabHost.addTab(tabSpec, MainFragment.class, null);
//      tabView1.getLayoutParams().height = 140;


        View tabView2 = mLayoutInflater.inflate(R.layout.activity_main_tab_item, null);
        final ImageView tabImage2 = (ImageView) tabView2.findViewById(R.id.tab_image);
        tabImage2.setImageResource(R.drawable.society);
        final TextView tabText2 = (TextView) tabView2.findViewById(R.id.tab_text);
        tabText2.setText("创客社区");
        mTabHost.addTab(mTabHost.newTabSpec(TABS[1]).setIndicator(tabView2),
                SocietyFragment.class, null);


        View tabView3 = mLayoutInflater.inflate(R.layout.activity_main_tab_item, null);
        final ImageView tabImage3 = (ImageView) tabView3.findViewById(R.id.tab_image);
        tabImage3.setImageResource(R.drawable.me);
        final TextView tabText3 = (TextView) tabView3.findViewById(R.id.tab_text);
        tabText3.setText(getString(R.string.user_view_tab_text));
        mTabHost.addTab(mTabHost.newTabSpec(TABS[2])
                .setIndicator(tabView3), UserFragment.class, null);

//        View tabView4 = mLayoutInflater.inflate(R.layout.activity_main_tab_item, null);
//        final ImageView tabImage4 = (ImageView) tabView4.findViewById(R.id.tab_image);
//        tabImage4.setImageResource(R.drawable.more);
//        final TextView tabText4 = (TextView) tabView4.findViewById(R.id.tab_text);
//        tabText4.setText(getString(R.string.user_view_tab_text));
//
//        mTabHost.addTab(mTabHost.newTabSpec(TABS[3])
//                .setIndicator(tabView4), UserFragment.class, null);

        mTabHost.setCurrentTab(0);
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                int child = mTabMap.get(tabId);
                tabImage1.setImageResource(R.drawable.home);
                tabImage2.setImageResource(R.drawable.society);
                tabImage3.setImageResource(R.drawable.me);
                //    tabImage4.setImageResource(R.drawable.sblook);
                tabText1.setTextColor(tabText1.getResources().getColor(R.color.black));
                tabText2.setTextColor(tabText2.getResources().getColor(R.color.black));
                tabText3.setTextColor(tabText3.getResources().getColor(R.color.black));
                switch (child) {
                    case 0:
                        tabImage1.setImageResource(R.drawable.home_selected);
                        tabText1.setTextColor(tabText1.getResources().getColor(R.color.txtselect));
                        break;
                    case 1:
                        tabImage2.setImageResource(R.drawable.society_selected);
                        tabText2.setTextColor(tabText2.getResources().getColor(R.color.txtselect));
                        break;
                    case 2:
                        tabImage3.setImageResource(R.drawable.me_selected);
                        tabText3.setTextColor(tabText3.getResources().getColor(R.color.txtselect));
                        break;
//                    case 3:
//                        tabImage4.setImageResource(R.drawable.sblook_select);
////                        tabText3.setTextColor(COLOR_GREEN_01);
//                        break;
                }
            }
        });

        super.initview();
    }


    private void initTab(int tabimage, String tabtext) {
        View tabView1 = mLayoutInflater.inflate(R.layout.activity_main_tab_item, null);
        ImageView tabImage1 = (ImageView) tabView1.findViewById(R.id.tab_image);
        TextView tabText1 = (TextView) tabView1.findViewById(R.id.tab_text);
        tabImage1.setImageResource(tabimage);
        tabText1.setText(tabtext);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
     //  refWatcher.watch(this);
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            isExit = false;
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), R.string.EXITAPP, Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            System.exit(0);
        }
    }

    private boolean isNetWorkConnected(Context context){
        if(context != null){
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.
                    getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if(mNetworkInfo != null){
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    @Override
    protected MainPresenter createPresenter() {
        return null;
    }
}
